/**
 * @name: webglUtils
 * @author: suarezzhu
 * @date: 2023/10/12 10:58
 * @description：webglUtils
 * @update: 2023/10/12 10:58
 */

const createShader = (gl, type, sourceCode) => {
    const shader = gl.createShader(type) // 创建着色器对象
    gl.shaderSource(shader, sourceCode) // 提供着色器代码
    gl.compileShader(shader) // 编译 -> 生成着色器
    const success = gl.getShaderParameter(shader, gl.COMPILE_STATUS)
    if (success) return shader

    console.error('shader err >>>', type, gl.getShaderInfoLog(shader))
    gl.deleteShader(shader);
}

const createProgram = (gl, vertexShader, fragmentShader) => {
    const program = gl.createProgram();

    gl.attachShader(program, vertexShader);
    gl.attachShader(program, fragmentShader);
    gl.linkProgram(program);
    gl.useProgram(program);
    const success = gl.getProgramParameter(program, gl.LINK_STATUS);
    if (success) {
        return program;
    }

    console.error('program err >>>', gl.getProgramInfoLog(program));
    gl.deleteProgram(program);
}

export {
    createProgram,
    createShader,
}